package services

import (
	"gin-app/app/common/request"
	"gin-app/app/models"
	"gin-app/global"
)

type waterSourceService struct {
}

var WaterSourceService = new(waterSourceService)

// 新建零售部负责人
func (n waterSourceService) CreateRetailArea(params request.CreateRetailArea) (err error, retailArea *models.RetailArea) {
	retailArea = models.NewRetailArea(params.Name, params.RetailName, params.Number)
	err = global.App.DB.Create(retailArea).Error
	return
}

// 删除零售负责人
func (n waterSourceService) DeleteRetailArea(params request.DeleteRetailArea) (err error) {
	//err = global.App.DB.Find().Error
	retailArea := &models.RetailArea{}
	err = global.App.DB.Take(retailArea, params.ID).Error
	if err != nil {
		return
	}
	err = global.App.DB.Delete(retailArea).Error
	return
}

// 获取零售负责人列表
func (n waterSourceService) GetRetailAreaList() (err error, retailAreas []models.RetailArea) {
	err = global.App.DB.Find(&retailAreas).Error
	return
}

// 修改零售负责人
func (n waterSourceService) EditorRetailArea(params request.EditorRetailArea) (err error, retailArea models.RetailArea) {
	data := make(map[string]interface{})
	data["Name"] = params.Name
	data["RetailName"] = params.RetailName
	data["Number"] = params.Number
	err = global.App.DB.Model(models.RetailArea{}).Where("id=?", params.ID).Updates(data).Error
	if err != nil {
		return
	}

	retailArea.Name = params.Name
	retailArea.RetailName = params.RetailName
	retailArea.Number = params.Number
	return
}

// 新建水源部负责人
func (n waterSourceService) CreateWaterSourceArea(params request.CreateWaterSourceArea) (err error, waterSourceArea *models.WaterSourceArea) {
	waterSourceArea = models.NewWaterSourceArea(params.Name, params.SourceName, params.Number)
	err = global.App.DB.Create(waterSourceArea).Error
	return
}

// 删除水源负责人
func (n waterSourceService) DeleteWaterSourceArea(params request.DeleteWaterSourceArea) (err error) {
	//err = global.App.DB.Find().Error
	waterSourceArea := &models.WaterSourceArea{}
	err = global.App.DB.Take(waterSourceArea, params.ID).Error
	if err != nil {
		return
	}
	err = global.App.DB.Delete(waterSourceArea).Error
	return
}

// 获取水源负责人列表
func (n waterSourceService) GetWaterSourceAreaList() (err error, waterSourceAreas []models.WaterSourceArea) {
	err = global.App.DB.Find(&waterSourceAreas).Error
	return
}

// 修改水源负责人
func (n waterSourceService) EditorWaterSourceArea(params request.EditorWaterSourceArea) (err error, waterSourceArea models.WaterSourceArea) {
	//err = global.App.DB.Find().Error
	//err = global.App.DB.Take(&retailArea).Where("id=?", params.ID).Error
	//if err != nil {
	//	return
	//}

	data := make(map[string]interface{})
	data["Name"] = params.Name
	data["SourceName"] = params.SourceName
	data["Number"] = params.Number
	err = global.App.DB.Model(models.WaterSourceArea{}).Where("id=?", params.ID).Updates(data).Error
	if err != nil {
		return
	}

	waterSourceArea.Name = params.Name
	waterSourceArea.SourceName = params.SourceName
	waterSourceArea.Number = params.Number
	return
}
